<script>
import { defineComponent, reactive, toRefs, watch, onMounted } from "vue";

export default defineComponent({
  name: "ConfigInput",
  props: {
    width: {
      type: Number,
      default: 340,
    },
    value: {
      type: String,
      default: "",
    },
    appendText: {
      type: String,
      default: "",
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    label: {
      type: String,
    },
  },
  setup(props, { emit }) {
    const state = reactive({
      currentData: "",
    });

    watch(
      () => props.value,
      (e) => {
        state.currentData = e;
      }
    );

    watch(
      () => state.currentData,
      (e) => {
        emit("update:value", e);
      }
    );

    onMounted(() => {
      state.currentData = props.value;
    });

    return {
      ...toRefs(state),
    };
  },
});
</script>

<template>
  <div class="component-config-input-container">
    <div
      class="input-bound"
      :style="{ width: `${width}px` }"
      :class="disabled ? 'disabled' : ''"
    >
      <input class="input" v-model="currentData" :disabled="disabled" />
    </div>
    <div class="append-block">
      <span v-if="appendText !== ''">{{ appendText }}</span>
      <slot name="append" />
    </div>
  </div>
</template>

<style lang="scss">
.component-config-input-container {
  height: 28px;
  display: flex;
  align-items: center;

  .input-bound {
    border: 1px solid #a0a0a0;
    height: 100%;
    padding-left: 12px;
    background-color: #fff;

    &.disabled {
      border-color: #b4b4b4;
      background-color: #f2f2f2;
      color: #8597b1;
    }

    .input {
      width: 100%;
      height: 100%;
      border: none;
      outline: none;
      background: none;

      &:disabled {
        border: none;
        outline: none;
        background: none;
      }
    }
  }

  .append-block {
    font-size: 12px;
    color: #9d9d9c;
    margin-left: 12px;
  }
}
</style>
